class Solution {
    public int maxScore(int[] cardPoints, int k) {
        int max = 0,rsum=0,lsum=0,len=cardPoints.length;
        // 先求出右边的sum
        for (int i=len-1;i>=len - k;i--) { 
            rsum += cardPoints[i];
        }
        max = rsum;
        // 计算 左边(sum)+右边(rsum-cardPoints[len-k+i])
        for (int i=0;i<k;i++) {
            lsum +=  cardPoints[i];
            rsum = rsum - cardPoints[len - k+i]; 
            max = Math.max(max,rsum+lsum);
        }
        return max;
    }
}